package apptoko.com;

import java.util.Locale;

import android.content.res.Resources;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import apptoko.com.fragment.DownloadingFragment;
import apptoko.com.fragment.WishlistFragment;
import apptoko.com.util.Constants;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import apptoko.com.R;
import com.astuetz.viewpager.extensions.PagerSlidingTabStrip;

public class BookManageActivity extends SherlockFragmentActivity {

	private String[] TITLES;
	private Resources res;
	private String currentStore;
	private int indicatorColor;
    
    @Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
    	currentStore = getIntent().getStringExtra(Constants.STORE_KEY);
    	if(currentStore.equalsIgnoreCase("books")){
    		setTheme(R.style.Theme_Styled_Home);
        	setThemeLowEndDevice(R.drawable.action_bar_home);
        	indicatorColor = R.color.ebook_indicator;
        	getSupportActionBar().setTitle(R.string.my_books);
    	} else {
    		setTheme(R.style.Theme_Styled_Comic);
        	setThemeLowEndDevice(R.drawable.action_bar_comic);
        	indicatorColor = R.color.comic_indicator;
        	getSupportActionBar().setTitle(R.string.my_comics);
    	}
		super.onCreate(arg0);
		setContentView(R.layout.activity_manage);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		res = getResources();
		TITLES = new String[] {res.getString(R.string.downloads), res.getString(R.string.wishlist)};
		DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
		int pageMargin = dm.widthPixels / 48;
		FragmentPagerAdapter adapter = new ManageAdapter(getSupportFragmentManager());
        ViewPager pager = (ViewPager) findViewById(R.id.tab_pager);
        pager.setOffscreenPageLimit(3);
        pager.setAdapter(adapter);
        pager.setPageMargin(pageMargin);
        pager.setPageMarginDrawable(R.drawable.bg_pagemargin_xml);
        PagerSlidingTabStrip indicator = (PagerSlidingTabStrip) findViewById(R.id.tab_page_indicator);
        indicator.setViewPager(pager);
        indicator.setIndicatorColorResource(indicatorColor);
        int pixelSize = (int) (14 * dm.scaledDensity); 
        indicator.setTextSize(pixelSize);
	}
    
    private void setThemeLowEndDevice(int drawableId){
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(drawableId);
            bg.setTileModeX(TileMode.REPEAT);
            getSupportActionBar().setBackgroundDrawable(bg);
        }
	}
    
    private class ManageAdapter extends FragmentPagerAdapter {
    	
        public ManageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
        	if(position == 0){
        		DownloadingFragment downloading = new DownloadingFragment();
        		Bundle b = new Bundle();
        		b.putString(Constants.STORE_KEY, currentStore);
        		downloading.setArguments(b);
				return downloading;
        	} else {
        		WishlistFragment wishlist = new WishlistFragment();
        		Bundle b = new Bundle();
        		if(currentStore.equalsIgnoreCase("books")){
        			b.putString(Constants.STORE_KEY, "ebook");
        		} else {
        			b.putString(Constants.STORE_KEY, "comic");
        		}
        		wishlist.setArguments(b);
        		return wishlist;
        	}
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position].toUpperCase(Locale.US);
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
